package com.sarnath.sardoop.modules.hadoop.web;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.sarnath.sardoop.common.bean.Response;
import com.sarnath.sardoop.common.web.BaseController;
import com.sarnath.sardoop.modules.hadoop.entity.HdfsFileInfo;
import com.sarnath.sardoop.modules.hadoop.service.HdfsExplorerService;
import com.sarnath.sardoop.modules.sys.util.UserUtils;

/**
 * @RequestMapping中的 ** 都表示需要操作的文件路径（不包括用户主目录）
 * @author yingc
 *
 */
@Controller
@RequestMapping(value = "${adminPath}/hadoop/hdfs")
public class HdfsExplorerController extends BaseController {
	@Resource
	HdfsExplorerService hdfsExplorer;
	
	/**
	 * ls/{查看目录绝对路径}
	 * @param request
	 * @return
	 */
	@RequestMapping(value="ls/**")
	@ResponseBody
	public Response ls(HttpServletRequest request){
		Response res=new Response();
		try {
			String path=getMatchPath(request);
			HdfsFileInfo[] fInfos=hdfsExplorer.ls(path);
			res.put("files", fInfos);
		} catch (Exception e) {
			res.setStatus(1);
			res.setMsg(e.toString());
			logger.info("",e);
		}
		return res;
	}
	@RequestMapping(value="du/**")
	@ResponseBody
	public Response du(HttpServletRequest request){
		Response res=new Response();
		try {
			String path=getMatchPath(request);
			HdfsFileInfo[] fInfos=hdfsExplorer.du(path);
			res.put("files", fInfos);
		} catch (Exception e) {
			res.setStatus(1);
			res.setMsg(e.toString());
			logger.info("",e);
		}
		return res;
	}
	@RequestMapping(value="mkdir/**")
	@ResponseBody
	public Response mkdir(HttpServletRequest request){
		Response res=new Response();
		try {
			String path=getMatchPath(request);
			if(!hdfsExplorer.mkdir(path)){
				res.setStatus(1);
				res.setMsg("fail");
			};
		} catch (Exception e) {
			res.setStatus(1);
			res.setMsg(e.toString());
			logger.info("",e);
		}
		return res;
	}
	@RequestMapping(value="rm/**")
	@ResponseBody
	public Response rm(HttpServletRequest request){
		Response res=new Response();
		try {
			String path=getMatchPath(request);
			if(!hdfsExplorer.del(path)){
				res.setStatus(1);
				res.setMsg("fail");
			};
		} catch (Exception e) {
			res.setStatus(1);
			res.setMsg(e.toString());
			logger.info("",e);
		}
		return res;
	}
	@RequestMapping(value="cp",method = RequestMethod.POST)
	@ResponseBody
	public Response cp(@RequestParam String src,@RequestParam String dest){
		Response res=new Response();
		try {
			hdfsExplorer.cp(src,dest);
		} catch (Exception e) {
			res.setStatus(1);
			res.setMsg(e.toString());
			logger.info("",e);
		}
		return res;
	}
	@RequestMapping(value="mv",method = RequestMethod.POST)
	@ResponseBody
	public Response mv(@RequestParam String src,@RequestParam String dest){
		Response res=new Response();
		try {
			hdfsExplorer.mv(src,dest);
		} catch (Exception e) {
			res.setStatus(1);
			res.setMsg(e.toString());
			logger.info("",e);
		}
		return res;
	}
	/**
	 * type 0：文件不存在 1：目录 2：文件
	 * @param request
	 * @return
	 */
	@RequestMapping(value="type/**")
	@ResponseBody
	public Response type(HttpServletRequest request){
		Response res=new Response();
		try {
			String path=getMatchPath(request);
			res.put("type",hdfsExplorer.fileType(path));
		} catch (Exception e) {
			res.setStatus(1);
			res.setMsg("fail");
			logger.info("",e);
		}
		return res;
	}
	/**
	 * upload/{文件保存目录绝对路径}
	 * @param request 文件保存名称
	 * @param name 上传文件
	 * @param file
	 * @return
	 */
	@RequestMapping(value="upload/**",method = RequestMethod.POST)
	@ResponseBody
	public Response upload(HttpServletRequest request,@RequestParam("userId") String userId,@RequestParam("name") String name,@RequestParam("file") CommonsMultipartFile file){
		Response res=new Response();
		try {
			UserUtils.setRequestAttr("userId", userId);
			String path=getMatchPath(request);
			hdfsExplorer.create(path,name,file.getInputStream());
		} catch (Exception e) {
			res.setStatus(1);
			res.setMsg(e.toString());
			logger.info("",e);
		}
		return res;
	}
	@RequestMapping(value="download/**",produces="application/x-download")
	@ResponseBody
	public byte[] download(HttpServletRequest request, HttpServletResponse response){
		try {
			String path=getMatchPath(request);
			response.addHeader("Content-Disposition", "attachment;filename=\""+path.substring(path.lastIndexOf("/")+1)+"\""); 
			return hdfsExplorer.download(path);
		} catch (Exception e) {
			logger.info(e.getMessage());
			throw e;
		}
	}
}
